package com.ruangong.GitClient.GitHubAPI;

import com.ruangong.GitClient.GitHubAPI.MoMingUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class CodeBackups {
    //12345678你在这编了一大堆，然后要推送到自己的分支，在推送到主枝上。
//    //这个能用!!!
//    private static String getFileContentTrue(String fileUrl) {
//        StringBuilder contentBuilder = new StringBuilder();
//        try {
//            URL url = new URL(fileUrl);
////            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//            HttpURLConnection conn = MoMingUtil.createConnection(url, "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");
////            conn.setRequestMethod("GET");
//            conn.setRequestProperty("Accept", "application/vnd.github.v3.raw");
////            conn.setRequestProperty("Authorization", "token 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");
//
//
//            if (conn.getResponseCode() != 200) {
//                throw new RuntimeException("Failed : HTTP error code : "
//                        + conn.getResponseCode());
//            }
//
//            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//            String inputLine;
//            while ((inputLine = br.readLine()) != null) {
//                contentBuilder.append(inputLine).append("\n");
//            }
//            br.close();
//            conn.disconnect();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return contentBuilder.toString();
//    }
//
//
//    /**
//     * 创建一个HTTP连接。
//     *
//     * @param url 要连接的URL对象，不应为null。
//     * @return 返回一个{@link HttpURLConnection}对象，代表与指定URL的HTTP连接。
//     * @throws IOException 如果无法打开连接或存在其他I/O错误，则抛出此异常。
//     */
////    public static HttpURLConnection createConnection(URL url) throws IOException {
////        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
////        conn.setRequestMethod("GET");
////        conn.setRequestProperty("Accept", "application/vnd.github.v3+json");
////        conn.setRequestProperty("Authorization", "token " + "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");
////        return conn;
////    }
//
//
//
//    /**
//     * 打印文件的内容。
//     *
//     * @param fileUrl 文件的URL。
//     */
//    private static void printFile(String fileUrl) {
//        String fileContent = getFileContent(fileUrl);
////        System.out.println(fileContent);
//    }
//
//
//    private static void printDir(String dirUrl) {
//        String dirContent = getFileContent(dirUrl);
////        System.out.println(dirContent);
//    }
//
//    /**
//     * 获取文件的内容。
//     *
//     * @param fileUrl 文件的URL。
//     * @return 包含文件内容的字符串。
//     */
//    private static String getFileContent(String fileUrl) {
//        StringBuilder contentBuilder = new StringBuilder();
//        try {
//            URL url = new URL(fileUrl);
//            HttpURLConnection conn = MoMingUtil.createConnection(url, "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");
//
//
//            if (conn.getResponseCode() != 200) {
//                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
//            }
//
//            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//            String inputLine;
//            while ((inputLine = br.readLine()) != null) {
//                contentBuilder.append(inputLine).append("\n");
//            }
//            br.close();
//            conn.disconnect();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return contentBuilder.toString();
//    }


//import java.util.List;
//
//    public class GithubRepo {
//        private long id;
//        private String nodeId;
//        private String name;
//        private String fullName;
//        private boolean isPrivate;
//        private Owner owner;
//        private String htmlUrl;
//        private String description;
//        private boolean fork;
//        private String url;
//        // 省略了其它URL属性以保持示例简洁
//        private String createdAt;
//        private String updatedAt;
//        private String pushedAt;
//        private String gitUrl;
//        private int size;
//        private int stargazersCount;
//        private int watchersCount;
//        private String language;
//        private boolean hasIssues;
//        private boolean hasProjects;
//        private boolean hasDownloads;
//        private boolean hasWiki;
//        private int forksCount;
//        private int openIssuesCount;
//        private List<String> topics;
//        private String visibility;
//        private int forks;
//        private int openIssues;
//        private int watchers;
//        private String defaultBranch;
//
//        // 构造器
//        public GithubRepo(long id, String nodeId, String name, String fullName, boolean isPrivate, Owner owner, String htmlUrl, String description, boolean fork, String url, String createdAt, String updatedAt, String pushedAt, String gitUrl, int size, int stargazersCount, int watchersCount, String language, boolean hasIssues, boolean hasProjects, boolean hasDownloads, boolean hasWiki, int forksCount, int openIssuesCount, List<String> topics, String visibility, int forks, int openIssues, int watchers, String defaultBranch) {
//            this.id = id;
//            this.nodeId = nodeId;
//            this.name = name;
//            this.fullName = fullName;
//            this.isPrivate = isPrivate;
//            this.owner = owner;
//            this.htmlUrl = htmlUrl;
//            this.description = description;
//            this.fork = fork;
//            this.url = url;
//            this.createdAt = createdAt;
//            this.updatedAt = updatedAt;
//            this.pushedAt = pushedAt;
//            this.gitUrl = gitUrl;
//            this.size = size;
//            this.stargazersCount = stargazersCount;
//            this.watchersCount = watchersCount;
//            this.language = language;
//            this.hasIssues = hasIssues;
//            this.hasProjects = hasProjects;
//            this.hasDownloads = hasDownloads;
//            this.hasWiki = hasWiki;
//            this.forksCount = forksCount;
//            this.openIssuesCount = openIssuesCount;
//            this.topics = topics;
//            this.visibility = visibility;
//            this.forks = forks;
//            this.openIssues = openIssues;
//            this.watchers = watchers;
//            this.defaultBranch = defaultBranch;
//        }
//
//        // 这里是所有属性的getter和setter方法
//        // ...
//
//        public static class Owner {
//            private String login;
//            private long id;
//            private String nodeId;
//            private String avatarUrl;
//            private String htmlUrl;
//            // 省略了其它属性以保持示例简洁
//
//            // 构造器
//            public Owner(String login, long id, String nodeId, String avatarUrl, String htmlUrl) {
//                this.login = login;
//                this.id = id;
//                this.nodeId = nodeId;
//                this.avatarUrl = avatarUrl;
//                this.htmlUrl = htmlUrl;
//            }
//
//            // getter和setter方法
//            // ...
//        }
//
//        // 主类的其他部分...
//    }


//import java.io.BufferedReader;
//import java.io.InputStreamReader;
//import java.net.HttpURLConnection;
//import java.net.URL;
//
//    public class GitHubRepoSearch {
//
//        public static void main(String[] args) {
//            try {
//                // 设置搜索关键词
//                String keyword = "Android";
//                // 构建GitHub API搜索仓库的URL
//                String url = "https://api.github.com/search/repositories?q=" + keyword;
//
//                // 创建URL对象
//                URL obj = new URL(url);
//                // 打开连接
//                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//
//                // 设置请求方式为GET
//                con.setRequestMethod("GET");
//                // 添加请求头，User-Agent是必需的，可以替换为自己的GitHub用户名
//                con.setRequestProperty("User-Agent", "Mozilla/5.0");
//
//                // 获取响应码
//                int responseCode = con.getResponseCode();
//                System.out.println("Sending 'GET' request to URL : " + url);
//                System.out.println("Response Code : " + responseCode);
//
//                // 读取响应
//                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
//                String inputLine;
//                StringBuilder response = new StringBuilder();
//
//                while ((inputLine = in.readLine()) != null) {
//                    response.append(inputLine);
//                }
//                in.close();
//
//                // 打印结果
//                System.out.println(response.toString());
//
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }

//GitHubRepositoryV1.class
//package com.ruangong.GitClient.GitHubAPI;
//
//    /**
//     * Javabeans类：该类演示了如何使用GitHub API获取存储库中的文件和文件内容。
//     */
//
//    public class GitHubRepositoryV1 {
//    /*{
//            "id": 12544093,
//            "node_id": "MDEwOlJlcG9zaXRvcnkxMjU0NDA5Mw==",
//            "name": "Android",
//            "full_name": "hmkcode/Android",
//            "private": false,
//            "owner": {
//                "login": "hmkcode",
//                "id": 3790597,
//                "node_id": "MDQ6VXNlcjM3OTA1OTc=",
//                "avatar_url": "https://avatars.githubusercontent.com/u/3790597?v=4",
//                "gravatar_id": "",
//                "url": "https://api.github.com/users/hmkcode",
//                "html_url": "https://github.com/hmkcode",
//                "followers_url": "https://api.github.com/users/hmkcode/followers",
//                "following_url": "https://api.github.com/users/hmkcode/following{/other_user}",
//                "gists_url": "https://api.github.com/users/hmkcode/gists{/gist_id}",
//                "starred_url": "https://api.github.com/users/hmkcode/starred{/owner}{/repo}",
//                "subscriptions_url": "https://api.github.com/users/hmkcode/subscriptions",
//                "organizations_url": "https://api.github.com/users/hmkcode/orgs",
//                "repos_url": "https://api.github.com/users/hmkcode/repos",
//                "events_url": "https://api.github.com/users/hmkcode/events{/privacy}",
//                "received_events_url": "https://api.github.com/users/hmkcode/received_events",
//                "type": "User",
//                "site_admin": false
//            },
//            "html_url": "https://github.com/hmkcode/Android",
//            "description": "Android related examples",
//            "fork": false,
//            "url": "https://api.github.com/repos/hmkcode/Android",
//            "forks_url": "https://api.github.com/repos/hmkcode/Android/forks",
//            "keys_url": "https://api.github.com/repos/hmkcode/Android/keys{/key_id}",
//            "collaborators_url": "https://api.github.com/repos/hmkcode/Android/collaborators{/collaborator}",
//            "teams_url": "https://api.github.com/repos/hmkcode/Android/teams",
//            "hooks_url": "https://api.github.com/repos/hmkcode/Android/hooks",
//            "issue_events_url": "https://api.github.com/repos/hmkcode/Android/issues/events{/number}",
//            "events_url": "https://api.github.com/repos/hmkcode/Android/events",
//            "assignees_url": "https://api.github.com/repos/hmkcode/Android/assignees{/user}",
//            "branches_url": "https://api.github.com/repos/hmkcode/Android/branches{/branch}",
//            "tags_url": "https://api.github.com/repos/hmkcode/Android/tags",
//            "blobs_url": "https://api.github.com/repos/hmkcode/Android/git/blobs{/sha}",
//            "git_tags_url": "https://api.github.com/repos/hmkcode/Android/git/tags{/sha}",
//            "git_refs_url": "https://api.github.com/repos/hmkcode/Android/git/refs{/sha}",
//            "trees_url": "https://api.github.com/repos/hmkcode/Android/git/trees{/sha}",
//            "statuses_url": "https://api.github.com/repos/hmkcode/Android/statuses/{sha}",
//            "languages_url": "https://api.github.com/repos/hmkcode/Android/languages",
//            "stargazers_url": "https://api.github.com/repos/hmkcode/Android/stargazers",
//            "contributors_url": "https://api.github.com/repos/hmkcode/Android/contributors",
//            "subscribers_url": "https://api.github.com/repos/hmkcode/Android/subscribers",
//            "subscription_url": "https://api.github.com/repos/hmkcode/Android/subscription",
//            "commits_url": "https://api.github.com/repos/hmkcode/Android/commits{/sha}",
//            "git_commits_url": "https://api.github.com/repos/hmkcode/Android/git/commits{/sha}",
//            "comments_url": "https://api.github.com/repos/hmkcode/Android/comments{/number}",
//            "issue_comment_url": "https://api.github.com/repos/hmkcode/Android/issues/comments{/number}",
//            "contents_url": "https://api.github.com/repos/hmkcode/Android/contents/{+path}",
//            "compare_url": "https://api.github.com/repos/hmkcode/Android/compare/{base}...{head}",
//            "merges_url": "https://api.github.com/repos/hmkcode/Android/merges",
//            "archive_url": "https://api.github.com/repos/hmkcode/Android/{archive_format}{/ref}",
//            "downloads_url": "https://api.github.com/repos/hmkcode/Android/downloads",
//            "issues_url": "https://api.github.com/repos/hmkcode/Android/issues{/number}",
//            "pulls_url": "https://api.github.com/repos/hmkcode/Android/pulls{/number}",
//            "milestones_url": "https://api.github.com/repos/hmkcode/Android/milestones{/number}",
//            "notifications_url": "https://api.github.com/repos/hmkcode/Android/notifications{?since,all,participating}",
//            "labels_url": "https://api.github.com/repos/hmkcode/Android/labels{/name}",
//            "releases_url": "https://api.github.com/repos/hmkcode/Android/releases{/id}",
//            "deployments_url": "https://api.github.com/repos/hmkcode/Android/deployments",
//            "created_at": "2013-09-02T16:12:28Z",
//            "updated_at": "2024-03-22T10:51:56Z",
//            "pushed_at": "2023-12-24T16:53:32Z",
//            "git_url": "git://github.com/hmkcode/Android.git",
//            "ssh_url": "git@github.com:hmkcode/Android.git",
//            "clone_url": "https://github.com/hmkcode/Android.git",
//            "svn_url": "https://github.com/hmkcode/Android",
//            "homepage": null,
//            "size": 1841,
//            "stargazers_count": 3496,
//            "watchers_count": 3496,
//            "language": "Java",
//            "has_issues": true,
//            "has_projects": true,
//            "has_downloads": true,
//            "has_wiki": true,
//            "has_pages": false,
//            "has_discussions": false,
//            "forks_count": 3411,
//            "mirror_url": null,
//            "archived": false,
//            "disabled": false,
//            "open_issues_count": 70,
//            "license": null,
//            "allow_forking": true,
//            "is_template": false,
//            "web_commit_signoff_required": false,
//            "topics": [
//
//            ],
//            "visibility": "public",
//            "forks": 3411,
//            "open_issues": 70,
//            "watchers": 3496,
//            "default_branch": "master",
//            "score": 1
//        }*/
//
//        /**
//         * GitHub API的访问令牌。
//         */
//        private String token;
//
//        private String repoOwner;
//
//        private String repoName;
//
//        private String contents_url;
//
//        GitHubRepositoryV1() {
//
//        }
//
//        GitHubRepositoryV1(String token, String repoOwner, String repoName) {
//            this.token = token;
//            this.repoOwner = repoOwner;
//            this.repoName = repoName;
//            this.contents_url = "https://api.github.com/repos/" + repoOwner + "/" + repoName + "/contents/";
//        }
//
//        public String getToken() {
//            return token;
//        }
//
//        public void setToken(String token) {
//            this.token = token;
//        }
//
//        public String getRepoOwner() {
//            return repoOwner;
//        }
//
//        public void setRepoOwner(String repoOwner) {
//            this.repoOwner = repoOwner;
//        }
//
//        public String getRepoName() {
//            return repoName;
//        }
//
//        public void setRepoName(String repoName) {
//            this.repoName = repoName;
//        }
//
//        public String getContents_url() {
//            return contents_url;
//        }
//
//        public void setContents_url(String contents_url) {
//            this.contents_url = contents_url;
//        }
//    }




/*
package com.ruangong.GitClient.app;

import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import com.ruangong.GitClient.GitHubAPI.MoMingUtil;
import com.ruangong.GitClient.GitHubAPI.NetworkUtils;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class OAuthRedirectActivity extends AppCompatActivity {
    private static final String CLIENT_ID = "Iv1.66524b24eade259a";
    private static final String REDIRECT_URI = "myapp://oauth";
    private static final String SCOPE = "repo,admin:org,admin:public_key,admin:repo_hook,admin:org_hook,gist,notifications,user,delete_repo,write:discussion";
    private static final String AUTH_URL = "https://github.com/login/oauth/authorize";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Uri uri = getIntent().getData();
        if (uri != null && uri.toString().startsWith("myapp://oauth")) {
            String code = uri.getQueryParameter("code");
            if (code != null) {
                NetworkUtils.fetchAccessToken(code, new Callback() {
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()) {
                            // 处理响应体，例如提取和保存令牌
                            String responseBody = response.body().string();
                            // 这里可以添加代码来处理获取的访问令牌
                            Log.d("OAuthRedirectActivity", "Access token: " + responseBody);
                            Map<String, String> params = new HashMap<>();
                            String[] pairs = responseBody.split("&");
                            for (String pair : pairs) {
                                String[] keyValue = pair.split("=");
                                if (keyValue.length > 1) {
                                    params.put(keyValue[0], keyValue[1]);
                                } else {
                                    params.put(keyValue[0], ""); // Assume empty string if no value is present
                                }
                            }

                            String accessToken = params.get("access_token");
                            System.out.println("Access token: " + accessToken);
                            String refreshToken = params.get("refresh_token");
                            System.out.println("Refresh token: " + refreshToken);
                            int expiresIn = Integer.parseInt(params.get("expires_in"));
                            System.out.println("Expires in: " + expiresIn);
                            int refreshTokenExpiresIn = Integer.parseInt(params.get("refresh_token_expires_in"));
                            System.out.println("Refresh token expires in: " + refreshTokenExpiresIn);

//                            MoMingUtil.fetchPrivateRepos(accessToken);
                            OkHttpClient client = new OkHttpClient();
                            Request request = new Request.Builder()
                                    .url("https://api.github.com/repos/SilverXQI/Silver_Python")
                                    .header("Authorization", "Bearer " + accessToken)
                                    .build();

                            client.newCall(request).enqueue(new Callback() {
                                @Override
                                public void onResponse(Call call, Response response) throws IOException {
                                    if (response.isSuccessful()) {
                                        String responseBody = response.body().string();
                                        System.out.println("Private Repos: " + responseBody);
                                        Log.d("OAuthRedirectActivity", "Private Repos: " + responseBody);
                                    } else {
                                        System.out.println("Failed to fetch private repos: " + response.code());
                                        Log.e("OAuthRedirectActivity", "Failed to fetch private repos: " + response.code());
                                    }
                                }

                                @Override
                                public void onFailure(Call call, IOException e) {
                                    e.printStackTrace();
                                }
                            });
                        } else {
                            // 处理错误响应
                            Log.e("OAuthRedirectActivity", "Failed to fetch access token");
                        }
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        // 处理网络请求失败的情况
                    }
                });
            }
        }
        finish(); // 关闭活动
    }
}

 */

















































}
